import { reqGetGoodsList, reqGetOneTypeList, reqGetTwoTypeList, reqGetTypeCount, reqGetGoodsListByData } from '@/api/goods'
export default {
  namespaced: true,
  state: () => ({
    goodsList: [],
    pagination: {
      currentPage: 1,
      pageSize: 7,
      total: 0
    },
    goods: {},
    oneTypeList: [],
    twoTypeList: [],
    typePagination: {
      currentPage: 1,
      pageSize: 7,
      total: 0
    },
    oneCount: 0,
    twoCount: 0,
    totalGap: 0,
    category: {},
    goodsSearch: {
      oneTypeList: [],
      twoTypeList: []
    },
    goodsSearchParams: {}
  }),
  mutations: {
    // 保存商品列表
    GETGOODSLIST(state, goodsList) {
      state.goodsList = [...goodsList]
    },
    // 保存分页数据
    SAVEPAGINATION(state, pagination) {
      state.pagination.currentPage = pagination.currentPage
      state.pagination.pageSize = pagination.pageSize
      state.pagination.total = pagination.total
    },
    // 保存修改商品的信息
    SAVEGOODS(state, goods) {
      state.goods = { ...goods }
    },
    // 保存一级商品类别
    GETONETYOELIST(state, oneTypeList) {
      state.oneTypeList = [...oneTypeList]
    },
    // 保存二级商品类别
    GETTWOTYOELIST(state, twoTypeList) {
      state.twoTypeList = [...twoTypeList]
    },
    // 保存类别分页数据
    SAVETYPEPAGINATION(state, pagination) {
      state.typePagination.currentPage = pagination.currentPage
      state.typePagination.pageSize = pagination.pageSize
      state.typePagination.total = pagination.total
    },
    // 保存oneCount
    SVAEONECOUNT(state, oneCount) {
      state.oneCount = oneCount
    },
    // 保存twoCount
    SVAETWOCOUNT(state, twoCount) {
      state.twoCount = twoCount
    },
    // 保存totalGap
    SAVETOTALGAP(state, totalGap) {
      state.totalGap = totalGap
    },
    // 保存修改的类别信息
    SAVECATEGOTY(state, category) {
      state.category = { ...category }
    },
    // 保存搜索的类别数据
    GETALLTYPELIST(state, { oneTypeList, twoTypeList }) {
      state.goodsSearch.oneTypeList = [...oneTypeList]
      state.goodsSearch.twoTypeList = [...twoTypeList]
    },
    // 保存商品查询数据
    SAVEGOODSSEARCHPARAMS(state, goodsSearchParams) {
      state.goodsSearchParams = { ...goodsSearchParams }
    }
  },
  actions: {
    // 获取商品列表
    async getGoodsList({ commit }, { pagination, data }) {
      let result
      const { currentPage, pageSize } = pagination.value
      const arr = Object.keys(data)
      if (arr.length !== 0) {
        commit('SAVEGOODSSEARCHPARAMS', data)
        result = await reqGetGoodsListByData({
          data,
          currentPage,
          pageSize
        })
      } else {
        result = await reqGetGoodsList({ currentPage, pageSize })
      }
      if (result.code === 200) {
        commit('GETGOODSLIST', result.data.data)
        commit('SAVEPAGINATION', {
          currentPage,
          pageSize,
          total: result.data.length
        })
        return 'OK'
      } else {
        return Promise.reject(new Error('获取商品列表出错！'))
      }
    },
    // 获取类别数量
    async getTypeCount({ commit }) {
      const result = await reqGetTypeCount()
      if (result.code === 200) {
        commit('SVAEONECOUNT', result.data.one)
        commit('SVAETWOCOUNT', result.data.two)
        return 'OK'
      } else {
        return Promise.reject(new Error('获取类别数量出错！'))
      }
    },
    // 获取一级类别
    async getOneTypeList({ commit }, { pagination }) {
      const { currentPage, pageSize } = pagination.value
      const result = await reqGetOneTypeList({ currentPage, pageSize })
      if (result.code === 200) {
        commit('GETONETYOELIST', result.data.data)
        commit('SAVETYPEPAGINATION', {
          currentPage,
          pageSize,
          total: result.data.length
        })
        commit('SVAEONECOUNT', result.data.length)
        return 'OK'
      } else {
        return Promise.reject(new Error('获取一级类别出错！'))
      }
    },
    // 获取二级类别
    async getTwoTypeList({ commit }, { pagination, qsGap, oneTotalPage, totalGap }) {
      let result
      const { currentPage, pageSize } = pagination.value
      if (qsGap < pageSize) {
        result = await reqGetTwoTypeList({ currentPage: currentPage - oneTotalPage > 0 ? currentPage - oneTotalPage : 1, pageSize: qsGap, totalGap })
      } else {
        result = await reqGetTwoTypeList({ currentPage: currentPage - oneTotalPage - 1 > 0 ? currentPage - oneTotalPage - 1 : 1, pageSize, totalGap })
      }
      if (result.code === 200) {
        commit('GETTWOTYOELIST', result.data.data)
        commit('SAVETYPEPAGINATION', {
          currentPage,
          pageSize,
          total: result.data.length
        })
        commit('SVAETWOCOUNT', result.data.length)
        return 'OK'
      } else {
        return Promise.reject(new Error('获取二级类别列表出错！'))
      }
    },
    // 获取全部类别
    async getTypeList({ commit, dispatch, state }, { pagination }) {
      const { currentPage, pageSize } = pagination.value
      const { oneCount, totalGap } = state
      const gap = pageSize - (oneCount % pageSize)
      commit('SAVETOTALGAP', gap)
      if (currentPage * pageSize <= oneCount) {
        commit('GETTWOTYOELIST', [])
        // 请求一级类别(currentPage, pageSize)
        await dispatch('getOneTypeList', { pagination: pagination })
      } else {
        const qsGap = pageSize * currentPage - oneCount
        const oneTotalPage = Math.floor(oneCount / pageSize)
        if (qsGap < pageSize) {
          // 请求一级类别(currentPage, pageSize)
          await dispatch('getOneTypeList', { pagination: pagination })
          // 请求二级类别(currentPage - oneTotalPage, qsGap)
          await dispatch('getTwoTypeList', {
            pagination,
            qsGap,
            oneTotalPage,
            totalGap: 0
          })
        } else {
          commit('GETONETYOELIST', [])
          // 请求二级类别(currentPage - oneTotalPage - 1, pageSize, totalGap)
          await dispatch('getTwoTypeList', {
            pagination,
            qsGap,
            oneTotalPage,
            totalGap
          })
        }
      }
    },
    // 获取全部的一二级类别
    async getAllTypeList({ commit }) {
      const oneResult = await reqGetOneTypeList({ currentPage: 1, pageSize: 100 })
      const twoResult = await reqGetTwoTypeList({ currentPage: 1, pageSize: 200, totalGap: 0 })
      if (oneResult.code === 200 && twoResult.code === 200) {
        commit('GETALLTYPELIST', {
          oneTypeList: oneResult.data.data,
          twoTypeList: twoResult.data.data
        })
        return 'OK'
      } else {
        return Promise.reject(new Error('获取类别数据出错！'))
      }
    }
  }
}
